package com.sunfeng.netty.atguigu.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * 协议包的解码器
 * @author sunfeng
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder 被调用");
        int readInt = in.readInt();
        byte[] bytes = new byte[readInt];
        in.readBytes(bytes);
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(readInt);
        messageProtocol.setContent(bytes);
        out.add(messageProtocol);
    }
}
